package de.voicegym.voicegym.recordings


import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import de.voicegym.voicegym.R
import de.voicegym.voicegym.databinding.FragmentRecordingsBinding
import de.voicegym.voicegym.menu.NavigationDrawerActivity
import de.voicegym.voicegym.model.Recording
import de.voicegym.voicegym.recordActivity.RecordActivity
import de.voicegym.voicegym.util.ISetTextable
import kotlinx.android.extensions.LayoutContainer
import java.text.SimpleDateFormat
import java.util.*

/**
 * [RecyclerView.Adapter] that can display a [Recording] and makes a call to the
 * specified [OnListFragmentInteractionListener].
 * TODO: Replace the implementation with code for your data type.
 */
class RecordingsAdapter(
    private val context: Context,
    private var values: List<Recording>,
    private val listInteractionListener: ListRecordingsFragment.ListInteractionListener
) : RecyclerView.Adapter<RecordingsAdapter.ViewHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.fragment_recordings, parent, false)
        return ViewHolder(context, view, listInteractionListener)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = values[position]
        holder.bindRecording(item)

        with(holder.containerView) {
            tag = item
        }
    }

    override fun getItemCount(): Int = values.size

    fun update(recordings: List<Recording>) {
        this.values = recordings
        notifyDataSetChanged()
    }

    operator fun get(position: Int): Recording = values[position]

    class ViewHolder(
        private val context: Context,
        override val containerView: View,
        private val listener: ListRecordingsFragment.ListInteractionListener
    ) : RecyclerView.ViewHolder(containerView), LayoutContainer {

        public lateinit var recordingsForeground: ConstraintLayout

        fun bindRecording(recording: Recording) {

            val bind = FragmentRecordingsBinding.bind(containerView)
            recordingsForeground = bind.recordingsForeground
            bind.durationView.text = " ${recording.duration}sec"
            val dateString = recording.fileName
                .split("/")
                .last()
                .replace("_", " ")
                .dropLast(4)


            val date =
                SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss", Locale.getDefault()).parse(dateString)
            val dateFormat = android.text.format.DateFormat.getDateFormat(context)
            val timeFormat = android.text.format.DateFormat.getTimeFormat(context)
            bind.createdView.text = dateFormat.format(date) + " - " + timeFormat.format(date)
            bind.nameView.text = when {
                recording.title == null -> dateString
                recording.title == "" -> dateString
                else -> recording.title
            }

            bind.nameView.setOnClickListener {
                val callback = object : ISetTextable {
                    override fun setText(text: String) {
                        RecordActivity.setNameOfFile(recording.fileName, text)
                    }

                }
                (context as NavigationDrawerActivity).showInputDialog(
                    bind.nameView.text.toString(),
                    callback
                )
            }
            bind.playAudioFileButton.setOnClickListener {
                listener.openAudioFileInPlaybackMode(recording.fileName)
            }

            setStar(bind.star1, recording.rating >= 1)
            setStarCallbackActions(bind.star1, recording.fileName, recording.rating, 1)
            setStar(bind.star2, recording.rating >= 2)
            setStarCallbackActions(bind.star2, recording.fileName, recording.rating, 2)
            setStar(bind.star3, recording.rating >= 3)
            setStarCallbackActions(bind.star3, recording.fileName, recording.rating, 3)
        }

        private fun setStar(star: ImageView, active: Boolean) {
            when (active) {
                true -> star.setImageResource(R.drawable.ic_star_on)
                false -> star.setImageResource(R.drawable.ic_star_off_bright_background)
            }
        }

        private fun setStarCallbackActions(
            star: ImageView,
            filename: String,
            currentRating: Int,
            targetRating: Int
        ) {
            star.setOnClickListener {
                RecordActivity.setRatingOfFile(
                    filename, when {
                        targetRating > currentRating -> targetRating
                        currentRating == targetRating -> targetRating - 1
                        else -> targetRating
                    }
                )
            }

        }
    }
}
